/**
 * 
 */
package xx.teach.entity;

import java.sql.SQLException;

import javax.sql.RowSet;

import xx.db.QueryRunner;
import xx.db.annotation.Column;
import xx.db.annotation.TableName;
import xx.db.sql.Owner;
import xx.db.sql.Table;

/**
 * @author Administrator 2014-3-18
 */
@TableName(name = "t_account")
public class Account extends Table
{

    /**
     * @param qr
     * @param owner
     */
    public Account(QueryRunner qr, Owner owner)
    {
        super(qr, owner);
        // TODO Auto-generated constructor stub
    }

    public static final String ACCOUNT_ID = "account_id";

    @Column(name = ACCOUNT_ID, pk = true)
    private long accountId;

    public static final String GROUP_ID = "group_id";

    @Column(name = GROUP_ID)
    private long groupId;

    public static final String PHONE = "phone";

    @Column(name = PHONE)
    private String phone;

    public static final String PASSWORD = "password";

    @Column(name = PASSWORD)
    private String password;

    public static final String TYPE = "type";

    @Column(name = TYPE)
    private int type;

    public static final String TEACHER_ID = "teacher_id";

    @Column(name = TEACHER_ID)
    private long teacherId;

    public static final String NAME = "name";

    @Column(name = NAME, unique = true)
    private String name;

    public long getAccountId()
    {
        return accountId;
    }

    public Account setAccountId(long accountId)
    {
        this.accountId = accountId;
        return this;
    }

    public long getGroupId()
    {
        return groupId;
    }

    public Account setGroupId(long groupId)
    {
        this.groupId = groupId;
        return this;
    }

    public String getPhone()
    {
        return phone;
    }

    public Account setPhone(String phone)
    {
        this.phone = phone;
        return this;
    }

    public String getPassword()
    {
        return password;
    }

    public Account setPassword(String password)
    {
        this.password = password;
        return this;
    }

    public int getType()
    {
        return type;
    }

    public Account setType(int type)
    {
        this.type = type;
        return this;
    }

    public long getTeacherId()
    {
        return teacherId;
    }

    public Account setTeacherId(long teacherId)
    {
        this.teacherId = teacherId;
        return this;
    }

    public String getName()
    {
        return name;
    }

    public Account setName(String name)
    {
        this.name = name;
        return this;
    }

    /*
     * (non-Javadoc)
     * @see xx.db.sql.Table#delete()
     */
    @Override
    public int delete() throws SQLException
    {
        // 删除相关的question
        delete.from(Question.class).where(Question.ACCOUNT_ID + "=" + accountId).go();

        // 删除相关的schedule
        delete.from(Schedule.class).where(Schedule.ACCOUNT_ID + "=" + accountId).go();

        return super.delete();
    }

    public RowSet getAccountAndGroup() throws SQLException
    {
        return select.from(Account.class).leftJoin(Group.class).using(GROUP_ID).where(NAME + "='" + name+"'").query();
    }
}
